package xhg.com.resell.utils;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import xhg.com.resell.base.activitybase.BaseActivity;


/**
 * @author dyd
 * Activity管理工具
 */
public class ActivityManager {
    private static List<WeakReference<Activity>> activities;

    private static ActivityManager instance;

    private ActivityManager() {
        activities = new ArrayList<>();
    }

    public static ActivityManager getInstance() {
        if (instance == null) {
            synchronized (ActivityManager.class) {
                if (instance == null) {
                    instance = new ActivityManager();
                }
            }
        }
        return instance;

    }

    public  List<WeakReference<Activity>> getActivities() {
        return activities;
    }

    /**
     * 添加Activity
     *
     * @param activity
     */
    public void add(Activity activity) {
        activities.add(new WeakReference<Activity>(activity));
    }


    /**
     * 移除Activity
     *
     * @param activity
     */
    public void remove(Activity activity) {
        for (WeakReference<Activity> temp : activities) {
            if (temp != null && temp.get() == activity) {
                activities.remove(temp);
                break;
            }
        }
    }


    /**
     * @param activityCls
     */
    public void remove(Class<?> activityCls) {
        for (Iterator<WeakReference<Activity>> iterator = activities.iterator(); iterator.hasNext(); ) {
            WeakReference<Activity> item = iterator.next();
            if (item != null && item.get() != null && item.get().getClass() == activityCls) {
                iterator.remove();
            }
        }
    }


    /**
     * 关闭指定 activity
     */
    public void finishActivity(BaseActivity... activities) {
        for (int i = 0; i < activities.length; i++) {
            if (null != activities[i]) {
                activities[i].finish();
            }
        }
    }

    /**
     * 关闭指定 activity(class)
     */
    public void finishActivity(Class<?>... activityClasses) {
        ArrayList<WeakReference<Activity>> waitfinish = new ArrayList<>();
        for (WeakReference<Activity> temp : activities) {
            for (int i = 0; i < activityClasses.length; i++) {
                if (null != temp.get() && temp.get().getClass() == activityClasses[i]) {
                    waitfinish.add(temp);
                    break;
                }
            }
        }
        for (WeakReference<Activity> activityWeakReference : waitfinish) {
            if (null != activityWeakReference.get()) {
                activityWeakReference.get().finish();
            }
        }
    }

    /**
     * 判断指定Activity是否存在
     */
    public Boolean isExist(Class<?> activityClass) {
        Boolean result = false;
        for (Iterator<WeakReference<Activity>> iterator = activities.iterator(); iterator.hasNext(); ) {
            WeakReference<Activity> item = iterator.next();
            if (null != item && null != item.get() && item.get().getClass() == activityClass) {
                result = true;
                break;
            }
        }
        return result;
    }
}
